/*    
 * Translation API wrapper for Google Desktop Translator
 * Copyright (C) 2009   Dusan Misic / MD Systems
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

/*
 * Translation API wrapper is using google-api-translate-java
 * by Richard Midwinter.
 * 
 * http://code.google.com/p/google-api-translate-java/
 */
package com.google.desktop.translator.engine;

import java.io.IOException;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

/**
 *
 * @author Dusan
 */
public class TranslationEngine {

    private String[] translationLanguages = {
        "Arabic",
        "Bulgarian",
        "Catalan",
        "Chinese",
        "Chinese simplified",
        "Chinese traditional",
        "Croatian",
        "Czech",
        "Danish",
        "Dutch",
        "English",
        "Filipino",
        "Finnish",
        "French",
        "German",
        "Greek",
        "Hebrew",
        "Hindi",
        "Indonesian",
        "Italian",
        "Japanese",
        "Korean",
        "Latvian",
        "Lithuanian",
        "Norwegian",
        "Polish",
        "Portugese",
        "Romanian",
        "Russian",
        "Serbian",
        "Slovak",
        "Slovenian",
        "Spanish",
        "Swedish",
        "Ukranian",
        "Vietnamese"
    };

    public String getTranslationLanguage(int number) {
        return this.translationLanguages[number];
    }

    public int getLanguageNumber() {
        int result = 0;
        result = translationLanguages.length;

        return result;
    }

    public String translateMagic(String inputText, String inputLanguage, String outputLanguage) throws IOException, ClassNotFoundException {

        String translatedText = null;

        try {
            translatedText = Translate.translate(inputText, this.languageTransformation(inputLanguage), this.languageTransformation(outputLanguage));
            return translatedText;
        } catch (ClassNotFoundException ex) {
            throw new ClassNotFoundException("Google Java API not found.");
        } catch (Exception ex) {
            throw new IOException("Google servers not reachable.");
        }
    }

    private String languageTransformation(String inputLanguage) {
        String result = null;

        if (inputLanguage == "Arabic") {
            result = Language.ARABIC;
        } else if (inputLanguage == "Bulgarian") {
            result = Language.BULGARIAN;
        } else if (inputLanguage == "Catalan") {
            result = Language.CATALAN;
        } else if (inputLanguage == "Chinese") {
            result = Language.CHINESE;
        } else if (inputLanguage == "Chinese simplified") {
            result = Language.CHINESE_SIMPLIFIED;
        } else if (inputLanguage == "Chinese traditional") {
            result = Language.CHINESE_TRADITIONAL;
        } else if (inputLanguage == "Croatian") {
            result = Language.CROATIAN;
        } else if (inputLanguage == "Czech") {
            result = Language.CZECH;
        } else if (inputLanguage == "Danish") {
            result = Language.DANISH;
        } else if (inputLanguage == "Dutch") {
            result = Language.DUTCH;
        } else if (inputLanguage == "English") {
            result = Language.ENGLISH;
        } else if (inputLanguage == "Filipino") {
            result = Language.FILIPINO;
        } else if (inputLanguage == "Finnish") {
            result = Language.FINNISH;
        } else if (inputLanguage == "French") {
            result = Language.FRENCH;
        } else if (inputLanguage == "German") {
            result = Language.GERMAN;
        } else if (inputLanguage == "Greek") {
            result = Language.GREEK;
        } else if (inputLanguage == "Hebrew") {
            result = Language.HEBREW;
        } else if (inputLanguage == "Hindi") {
            result = Language.HINDI;
        } else if (inputLanguage == "Indonesian") {
            result = Language.INDONESIAN;
        } else if (inputLanguage == "Italian") {
            result = Language.ITALIAN;
        } else if (inputLanguage == "Japanese") {
            result = Language.JAPANESE;
        } else if (inputLanguage == "Korean") {
            result = Language.KOREAN;
        } else if (inputLanguage == "Latvian") {
            result = Language.LATVIAN;
        } else if (inputLanguage == "Lithuanian") {
            result = Language.LITHUANIAN;
        } else if (inputLanguage == "Norwegian") {
            result = Language.NORWEGIAN;
        } else if (inputLanguage == "Polish") {
            result = Language.POLISH;
        } else if (inputLanguage == "Portugese") {
            result = Language.PORTUGESE;
        } else if (inputLanguage == "Romanian") {
            result = Language.ROMANIAN;
        } else if (inputLanguage == "Russian") {
            result = Language.RUSSIAN;
        } else if (inputLanguage == "Serbian") {
            result = Language.SERBIAN;
        } else if (inputLanguage == "Slovak") {
            result = Language.SLOVAK;
        } else if (inputLanguage == "Slovenian") {
            result = Language.SLOVENIAN;
        } else if (inputLanguage == "Spanish") {
            result = Language.SPANISH;
        } else if (inputLanguage == "Swedish") {
            result = Language.SWEDISH;
        } else if (inputLanguage == "Ukranian") {
            result = Language.UKRANIAN;
        } else if (inputLanguage == "Vietnamese") {
            result = Language.VIETNAMESE;
        }

        return result;
    }
}
